#
#   快速建站 v0.0.2
#   
#   使用前提： CentOS 7
#
#   使用方法： ./create-site.sh "tokyo2.jianpuwang.cn"





# 变量
# 网站名
site_name="tokyo2.jianpuwang.cn"
# 端口
# 端口自动使用8890开始找未使用的端口
site_port=8890
# APISIX端口
apisix_port=8080

#节点端口
node_ip="127.0.0.1"

# 使用https
use_https=1

# APISIX的X-API-KEY
#api_key="edd1c9f034335f136f87ad84b625oren"
api_key="edd1c9f034335f136f87ad84b625c8f1"

# 邮箱必填
email="850657663@qq.com"

# 判断参数
if test -n $1
then
  site_name=$1
  echo "使用网站名：$site_name"
fi



# 判断端口
while [ "$(netstat -tulnp | grep -c $site_port)" -ge 1 ];do
 site_port=$[$site_port+1]
 echo $site_port
done

echo "使用端口$site_port"



# 安装Docker
if test -e /usr/bin/docker
then
  # 检查 APISIX 的版本号
  docker version
  echo 'Docker已存在，跳过安装'
else
  yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
  sudo yum install -y docker-ce docker-ce-cli containerd.io
  service docker start
fi

# 安装APISIX
if test -e /usr/bin/apisix
then
  # 检查 APISIX 的版本号
  apisix version
  echo 'Apisix已存在，跳过安装'
else
  # 安装 etcd
  wget https://ghproxy.com/https://github.com/etcd-io/etcd/releases/download/v3.4.13/etcd-v3.4.13-linux-amd64.tar.gz
  tar -xvf etcd-v3.4.13-linux-amd64.tar.gz && \
      cd etcd-v3.4.13-linux-amd64 && \
      sudo cp -a etcd etcdctl /usr/bin/

  # 添加 OpenResty 源
  sudo yum install -y yum-utils
  sudo yum-config-manager --add-repo https://openresty.org/package/centos/openresty.repo

  # 安装 OpenResty 和 编译工具
  sudo yum install -y openresty curl git gcc openresty-openssl111-devel unzip pcre pcre-devel

  # 安装 LuaRocks
  curl https://raw.githubusercontent.com/apache/apisix/master/utils/linux-install-luarocks.sh -sL | bash -

  # 开启 etcd server
  nohup etcd &

  sudo yum install -y https://ghproxy.com/https://github.com/apache/apisix/releases/download/2.10.1/apisix-2.10.1-0.el7.x86_64.rpm

  cat > /usr/local/apisix/conf/config.yaml<< EOF
apisix:
  node_listen:
    - ${apisix_port}
  admin_key:
    - name: admin
      key: $api_key
      role: admin
  ssl:
    listen:
      - 443
EOF

  # 检查 APISIX 的版本号
  apisix version

  # 启动 APISIX
  apisix start
fi

# 创建一个默认的Openresty服务器
docker create --name openresty-office openresty/openresty:alpine
docker stop openresty-office


# 创建站点
cd $HOME/
mkdir ${site_name}
cd ${site_name}
mkdir html
mkdir conf.d

# 复制配置文件
docker cp openresty-office:/etc/nginx/conf.d/default.conf $HOME/${site_name}/conf.d
# 复制默认页面
docker cp openresty-office:/usr/local/openresty/nginx/html $HOME/${site_name}/

# 创建网站的nginx容器
docker run --name ${site_name} -d -p ${site_port}:80 -v $HOME/${site_name}/conf.d:/etc/nginx/conf.d -v $HOME/${site_name}/html:/usr/local/openresty/nginx/html openresty/openresty:alpine

# 注册节点
# TODO 远程的后端接口，也通过这个走https
curl "http://127.0.0.1:${apisix_port}/apisix/admin/upstreams/${site_port}" -H "X-API-KEY: ${api_key}" -X PUT -d "{\"type\": \"roundrobin\",   \"nodes\": {     \"$node_ip:${site_port}\": 1   } }"
# 注册域名解析
curl "http://127.0.0.1:${apisix_port}/apisix/admin/routes/${site_port}" -H "X-API-KEY: ${api_key}" -X PUT -d "{\"uri\": \"/*\",   \"hosts\": [\"www.${site_name}\", \"${site_name}\"],   \"upstream_id\": \"${site_port}\" }"

if [ use_https == 1 ]
then

  # 安装证书
  if test -e $HOME/.acme.sh/acme.sh
  then
    echo "已存在acme.sh"
  else
    echo "安装acme.sh"
    yum install -y socat
    curl  https://get.acme.sh | sh -s email=${email}
  fi

  sleep 5s

  # 生成证书
  echo "正在生成证书"
  sudo "$HOME/.acme.sh/acme.sh" --issue -d "${site_name}" -w $HOME/${site_name}/html -k ec-256 --server letsencrypt
  #sudo "$HOME/.acme.sh/acme.sh" --issue -d "${site_name}" --standalone -k ec-256 --server letsencryp --listen-v6 --httpport 88 --tlsport 8443
  echo "证书已生成"
  # 生成crt文件
  mkdir -p $HOME/${site_name}/tls
  sudo "$HOME/.acme.sh/acme.sh" --installcert -d "${site_name}" --fullchainpath "$HOME/${site_name}/tls/${site_name}.crt" --keypath "$HOME/${site_name}/tls/${site_name}.key" --ecc
  echo "CRT证书已生成"

  # crt文件注册到APISIX
  # TODO 此处可能会过期
  if test -e $HOME/${site_name}/tls/${site_name}.crt
  then
    acme_crt=$(cat $HOME/${site_name}/tls/${site_name}.crt)
    acme_key=$(cat $HOME/${site_name}/tls/${site_name}.key)
    # 证书内容上传APISIX
    echo "证书内容开始上传APISIX"
    curl "http://127.0.0.1:${apisix_port}/apisix/admin/ssl/${site_port}" -H "X-API-KEY: ${api_key}" -X PUT -d "{\"cert\":\"$acme_crt\",\"key\":\"$acme_key\",\"snis\":[\"www.${site_name}\",\"${site_name}\"]}"
  else
    echo "不存在证书$HOME/${site_name}/tls/${site_name}.crt"
  fi
else
  echo "跳过证书安装"
fi


